/**
 * ISWE.
 * Created on : 22/07/2009
 *
 * Copyright (c) 2009 ISWE.
 * Rafael Noronha de Almeida
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of Rafael Noronha de Almeida
 * ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Rafael.
 */
package br.com.iswe.recipemaster.facade.security;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import br.com.iswe.core.exp.ISWEConfigurationException;
import br.com.iswe.core.jpa.JPAUtil;
import br.com.iswe.core.jpa.JPAUtilCallback;



/**
 *
 * Description: 
 *
 * @author Rafael Noronha de Almeida
 *
 * @version $Revision: 1.3 $ $Date: 2009/07/26 09:14:59 $
 *
 */
public class InitialSecurityConfigurationFacadeImpl  {

	
	private Log log = LogFactory.getLog(InitialSecurityConfigurationFacadeImpl.class);
	
	public InitialSecurityConfigurationFacadeImpl(final RoleFacade roleFacade, final UserFacade userFacade){
		
		JPAUtil.getInstance().executeInJPAContext(new JPAUtilCallback(){
			public void execute() {
				
				try{
					//check if the ROLE_ADMIN exists
					roleFacade.setupBasicRoles();
				}
				catch (Throwable exp){
					log.error("Error configuring the basic roles in the system", exp);
					throw new ISWEConfigurationException("Error configuring the basic roles in the system", exp);
				}	
				
				try{
					//check if the Admin user exists
					userFacade.setupAdminUser();
				}
				catch (Throwable exp){
					log.error("Error configuring the admin user into the system", exp);
					throw new ISWEConfigurationException("Error configuring the admin user into the system", exp);
				}
				
			}});
	}
}
